package com.yuwubao.hz_International.controllers.sys;

import com.yuwubao.hz_International.entities.*;
import com.yuwubao.hz_International.services.*;
import com.yuwubao.hz_International.util.Const;
import com.yuwubao.hz_International.util.RestApiResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by yangyu on 2018/1/28.
 */
@RestController
@RequestMapping("/sys/eventManage")
@Transactional
@CrossOrigin
@Api(value = "后台接口")
public class EventManageController {

    private final static Logger logger = LoggerFactory.getLogger(EventManageController.class);

    @Autowired
    private RechargeConfigService rechargeConfigService;

    @Autowired
    private WelfareConfigService welfareConfigService;

    @Autowired
    private GiftConfigService giftConfigService;

    @Autowired
    private InviteRewardsConfigService inviteRewardsConfigService;

    @Autowired
    private BannerImgService bannerImgService;

    /**
     * 获取充值配置
     * @return
     */
    @GetMapping("/getRechargeConfig")
    @ApiOperation(value = "获取充值配置")
    public RestApiResponse<RechargeConfigEntity> getRechargeConfig() {
        RestApiResponse<RechargeConfigEntity> result = new RestApiResponse<RechargeConfigEntity>();
        try {
            List<RechargeConfigEntity> rechargeConfigEntityList = rechargeConfigService.findAll();
            if (rechargeConfigEntityList.size() > 0) {
                result.successResponse(Const.SUCCESS, rechargeConfigEntityList.get(0));
            }
        } catch (Exception e) {
            logger.warn("获取充值配置异常", e);
            result.failedApiResponse(Const.FAILED, "获取充值配置异常");
        }
        return result;
    }

    /**
     * 添加充值配置
     * @param rechargeConfig
     * @return
     */
    @PostMapping("/addRechargeConfig")
    @ApiOperation(value = "添加充值配置")
    public RestApiResponse<RechargeConfigEntity> addRechargeConfig(@RequestBody RechargeConfigEntity rechargeConfig) {
        RestApiResponse<RechargeConfigEntity> result = new RestApiResponse<RechargeConfigEntity>();
        try {
            List<RechargeConfigEntity> rechargeConfigEntityList = rechargeConfigService.findAll();
            RechargeConfigEntity rechargeConfigEntity = null;
            if (rechargeConfigEntityList.size() == 0) {
                rechargeConfigEntity = rechargeConfigService.save(rechargeConfig);
            } else {
                RechargeConfigEntity entity = rechargeConfigEntityList.get(0);
                rechargeConfig.setId(entity.getId());
                rechargeConfigEntity = rechargeConfigService.update(rechargeConfig);
            }
            if (rechargeConfigEntity == null) {
                result.failedApiResponse(Const.FAILED, "充值活动配置失败");
                return result;
            }
            result.successResponse(Const.SUCCESS, rechargeConfigEntity, "配置成功");
        } catch (Exception e) {
            logger.warn("配置充值活动异常", e);
            result.failedApiResponse(Const.FAILED, "配置充值活动异常");
        }
        return result;
    }

    /**
     * 修改充值配置
     * @param rechargeConfig
     * @return
     */
    @PostMapping("/updateRechargeConfig")
    @ApiOperation(value = "修改充值配置")
    public RestApiResponse<RechargeConfigEntity> updateRechargeConfig(@RequestBody RechargeConfigEntity rechargeConfig) {
        RestApiResponse<RechargeConfigEntity> result = new RestApiResponse<RechargeConfigEntity>();
        try {
            RechargeConfigEntity rechargeConfigEntity = rechargeConfigService.update(rechargeConfig);
            if (rechargeConfigEntity == null) {
                result.failedApiResponse(Const.FAILED, "修改充值活动失败");
                return result;
            }
            result.successResponse(Const.SUCCESS, rechargeConfigEntity, "修改成功");
        } catch (Exception e) {
            logger.warn("修改充值活动异常", e);
            result.failedApiResponse(Const.FAILED, "修改充值活动异常");
        }
        return result;
    }

    /**
     * 获取福利金配置
     * @return
     */
    @GetMapping("/getWelfareConfig")
    @ApiOperation(value = "获取福利金配置")
    public RestApiResponse<WelfareConfigEntity> getWelfareConfig() {
        RestApiResponse<WelfareConfigEntity> result = new RestApiResponse<WelfareConfigEntity>();
        try {
            List<WelfareConfigEntity> welfareConfigEntityList = welfareConfigService.findAll();
            if (welfareConfigEntityList.size() > 0) {
                result.successResponse(Const.SUCCESS, welfareConfigEntityList.get(0));
            }
        } catch (Exception e) {
            logger.warn("获取福利金配置异常", e);
            result.failedApiResponse(Const.FAILED, "获取福利金配置异常");
        }
        return result;
    }

    /**
     * 添加福利金配置
     * @param welfareConfig
     * @return
     */
    @PostMapping("/addWelfareConfig")
    @ApiOperation(value = "添加福利金配置")
    public RestApiResponse<WelfareConfigEntity> addWelfareConfig(@RequestBody WelfareConfigEntity welfareConfig) {
        RestApiResponse<WelfareConfigEntity> result = new RestApiResponse<WelfareConfigEntity>();
        try {
            List<WelfareConfigEntity> welfareConfigEntityList = welfareConfigService.findAll();
            WelfareConfigEntity welfareConfigEntity = null;
            if (welfareConfigEntityList.size() == 0) {
                welfareConfigEntity = welfareConfigService.save(welfareConfig);
            } else {
                WelfareConfigEntity configEntity = welfareConfigEntityList.get(0);
                welfareConfig.setId(configEntity.getId());
                welfareConfigEntity = welfareConfigService.update(welfareConfig);
            }
            if (welfareConfigEntity == null) {
                result.failedApiResponse(Const.FAILED, "福利金活动配置失败");
                return result;
            }
            result.successResponse(Const.SUCCESS, welfareConfigEntity, "配置成功");
        } catch (Exception e) {
            logger.warn("配置福利金活动异常", e);
            result.failedApiResponse(Const.FAILED, "配置福利金活动异常");
        }
        return result;
    }

    /**
     * 修改福利金配置
     * @param welfareConfig
     * @return
     */
    @PostMapping("/updateWelfareConfig")
    @ApiOperation(value = "修改福利金配置")
    public RestApiResponse<WelfareConfigEntity> updateWelfareConfig(@RequestBody WelfareConfigEntity welfareConfig) {
        RestApiResponse<WelfareConfigEntity> result = new RestApiResponse<WelfareConfigEntity>();
        try {
            WelfareConfigEntity welfareConfigEntity = welfareConfigService.update(welfareConfig);
            if (welfareConfigEntity == null) {
                result.failedApiResponse(Const.FAILED, "修改福利金活动失败");
                return result;
            }
            result.successResponse(Const.SUCCESS, welfareConfigEntity, "修改成功");
        } catch (Exception e) {
            logger.warn("修改福利金活动异常", e);
            result.failedApiResponse(Const.FAILED, "修改福利金活动异常");
        }
        return result;
    }

    /**
     * 获取礼券配置
     * @return
     */
    @GetMapping("/getGiftConfig")
    @ApiOperation(value = "获取礼券配置")
    public RestApiResponse<GiftConfigEntity> getGiftConfig() {
        RestApiResponse<GiftConfigEntity> result = new RestApiResponse<GiftConfigEntity>();
        try {
            List<GiftConfigEntity> giftConfigEntityList = giftConfigService.findAll();
            if (giftConfigEntityList.size() > 0) {
                result.successResponse(Const.SUCCESS, giftConfigEntityList.get(0));
            }
        } catch (Exception e) {
            logger.warn("获取礼券配置异常", e);
            result.failedApiResponse(Const.FAILED, "获取礼券配置异常");
        }
        return result;
    }

    /**
     * 添加礼券配置
     * @param giftConfig
     * @return
     */
    @PostMapping("/addGiftConfig")
    @ApiOperation(value = "添加礼券配置")
    public RestApiResponse<GiftConfigEntity> addGiftConfig(@RequestBody GiftConfigEntity giftConfig) {
        RestApiResponse<GiftConfigEntity> result = new RestApiResponse<GiftConfigEntity>();
        try {
            List<GiftConfigEntity> giftConfigEntityList = giftConfigService.findAll();
            GiftConfigEntity giftConfigEntity = null;
            if (giftConfigEntityList.size() == 0) {
                giftConfigEntity = giftConfigService.save(giftConfig);
            } else {
                GiftConfigEntity configEntity = giftConfigEntityList.get(0);
                giftConfig.setId(configEntity.getId());
                giftConfigEntity = giftConfigService.update(giftConfig);
            }
            if (giftConfigEntity == null) {
                result.failedApiResponse(Const.FAILED, "礼券活动配置失败");
                return result;
            }
            result.successResponse(Const.SUCCESS, giftConfigEntity, "配置成功");
        } catch (Exception e) {
            logger.warn("配置礼券活动异常", e);
            result.failedApiResponse(Const.FAILED, "配置礼券活动异常");
        }
        return result;
    }

    /**
     * 修改礼券配置
     * @param giftConfig
     * @return
     */
    @PostMapping("/updateGiftConfig")
    @ApiOperation(value = "修改礼券配置")
    public RestApiResponse<GiftConfigEntity> updateWelfareConfig(@RequestBody GiftConfigEntity giftConfig) {
        RestApiResponse<GiftConfigEntity> result = new RestApiResponse<GiftConfigEntity>();
        try {
            GiftConfigEntity giftConfigEntity = giftConfigService.update(giftConfig);
            if (giftConfigEntity == null) {
                result.failedApiResponse(Const.FAILED, "修改礼券活动失败");
                return result;
            }
            result.successResponse(Const.SUCCESS, giftConfigEntity, "修改成功");
        } catch (Exception e) {
            logger.warn("修改礼券活动异常", e);
            result.failedApiResponse(Const.FAILED, "修改礼券活动异常");
        }
        return result;
    }

    /**
     * 获取邀请好友奖励配置
     * @return
     */
    @GetMapping("/getInviteRewardsConfig")
    @ApiOperation(value = "获取邀请好友奖励配置")
    public RestApiResponse<InviteRewardsConfigEntity> getInviteRewardsConfig() {
        RestApiResponse<InviteRewardsConfigEntity> result = new RestApiResponse<InviteRewardsConfigEntity>();
        try {
            List<InviteRewardsConfigEntity> inviteRewardsConfigEntityList = inviteRewardsConfigService.findAll();
            if (inviteRewardsConfigEntityList.size() > 0) {
                result.successResponse(Const.SUCCESS, inviteRewardsConfigEntityList.get(0));
            }
        } catch (Exception e) {
            logger.warn("获取邀请奖励配置异常", e);
            result.failedApiResponse(Const.FAILED, "获取邀请奖励配置异常");
        }
        return result;
    }

    /**
     * 添加邀请好友奖励配置
     * @param inviteRewardsConfig
     * @return
     */
    @PostMapping("/addInviteRewardsConfig")
    @ApiOperation(value = "添加邀请好友奖励配置")
    public RestApiResponse<InviteRewardsConfigEntity> addInviteRewardsConfig(@RequestBody InviteRewardsConfigEntity inviteRewardsConfig) {
        RestApiResponse<InviteRewardsConfigEntity> result = new RestApiResponse<InviteRewardsConfigEntity>();
        try {
            List<InviteRewardsConfigEntity> inviteRewardsConfigEntityList = inviteRewardsConfigService.findAll();
            InviteRewardsConfigEntity inviteRewardsConfigEntity = null;
            if (inviteRewardsConfigEntityList.size() == 0) {
                inviteRewardsConfigEntity = inviteRewardsConfigService.save(inviteRewardsConfig);
            } else {
                InviteRewardsConfigEntity configEntity = inviteRewardsConfigEntityList.get(0);
                inviteRewardsConfig.setId(configEntity.getId());
                inviteRewardsConfigEntity = inviteRewardsConfigService.update(inviteRewardsConfig);
            }
            if (inviteRewardsConfigEntity == null) {
                result.failedApiResponse(Const.FAILED, "邀请好友奖励配置失败");
                return result;
            }
            result.successResponse(Const.SUCCESS, inviteRewardsConfigEntity, "配置成功");
        } catch (Exception e) {
            logger.warn("配置邀请好友奖励异常", e);
            result.failedApiResponse(Const.FAILED, "配置邀请好友奖励异常");
        }
        return result;
    }

    /**
     * 修改邀请好友奖励配置
     * @param inviteRewardsConfig
     * @return
     */
    @PostMapping("/updateInviteRewardsConfig")
    @ApiOperation(value = "修改邀请好友奖励配置")
    public RestApiResponse<InviteRewardsConfigEntity> updateInviteRewardsConfig(@RequestBody InviteRewardsConfigEntity inviteRewardsConfig) {
        RestApiResponse<InviteRewardsConfigEntity> result = new RestApiResponse<InviteRewardsConfigEntity>();
        try {
            InviteRewardsConfigEntity inviteRewardsConfigEntity = inviteRewardsConfigService.update(inviteRewardsConfig);
            if (inviteRewardsConfigEntity == null) {
                result.failedApiResponse(Const.FAILED, "修改邀请好友奖励配置失败");
                return result;
            }
            result.successResponse(Const.SUCCESS, inviteRewardsConfigEntity, "修改成功");
        } catch (Exception e) {
            logger.warn("修改邀请好友奖励配置异常", e);
            result.failedApiResponse(Const.FAILED, "修改邀请好友奖励配置异常");
        }
        return result;
    }

    /**
     * 获取banner图
     * @return
     */
    @GetMapping("/getBannerImg")
    @ApiOperation(value = "获取banner图")
    public RestApiResponse<List<BannerImgEntity>> getBannerImg() {
        RestApiResponse<List<BannerImgEntity>> result = new RestApiResponse<List<BannerImgEntity>>();
        try {
            List<BannerImgEntity> bannerImgEntityList = bannerImgService.findAll();
            result.successResponse(Const.SUCCESS, bannerImgEntityList);
        } catch (Exception e) {
            logger.warn("获取banner图异常", e);
            result.failedApiResponse(Const.FAILED, "获取banner图异常");
        }
        return result;
    }

    /**
     * 添加banner图
     * @param bannerImgs
     * @return
     */
    @PostMapping("/addBannerImg")
    @ApiOperation(value = "添加banner图")
    public RestApiResponse<List<BannerImgEntity>> addBannerImg(@RequestBody String[] bannerImgs) {
        RestApiResponse<List<BannerImgEntity>> result = new RestApiResponse<List<BannerImgEntity>>();
        try {
            List<BannerImgEntity> list = new ArrayList<BannerImgEntity>();
            for (String imgUrl: bannerImgs){
                BannerImgEntity bannerImgEntity = new BannerImgEntity();
                bannerImgEntity.setBannerUrl(imgUrl);
                BannerImgEntity entity = bannerImgService.save(bannerImgEntity);
                list.add(entity);
            }
            if (list.size() == 0) {
                result.failedApiResponse(Const.FAILED, "添加banner图失败");
                return result;
            }
            result.successResponse(Const.SUCCESS, list, "添加成功");
        } catch (Exception e) {
            logger.warn("添加banner图异常", e);
            result.failedApiResponse(Const.FAILED, "添加banner图异常");
        }
        return result;
    }

    /**
     * 修改banner图
     * @param bannerImgEntityList
     * @return
     */
    @PostMapping("/updateBannerImg")
    @ApiOperation(value = "修改banner图")
    public RestApiResponse<List<BannerImgEntity>> updateBannerImg(@RequestBody List<BannerImgEntity> bannerImgEntityList) {
        RestApiResponse<List<BannerImgEntity>> result = new RestApiResponse<List<BannerImgEntity>>();
        try {
            List<BannerImgEntity> list = new ArrayList<BannerImgEntity>();
            for (BannerImgEntity bannerImgEntity: bannerImgEntityList){
                BannerImgEntity newBannerImgEntity = bannerImgService.update(bannerImgEntity);
                list.add(newBannerImgEntity);
            }
            if (list.size() == 0) {
                result.failedApiResponse(Const.FAILED, "修改banner图失败");
                return result;
            }
            result.successResponse(Const.SUCCESS, list, "修改成功");
        } catch (Exception e) {
            logger.warn("修改banner图异常", e);
            result.failedApiResponse(Const.FAILED, "修改banner图异常");
        }
        return result;
    }

    /**
     * 修改banner图
     * @param bannerImgId
     * @return
     */
    @PostMapping("/deleteBannerImg")
    @ApiOperation(value = "删除banner图")
    public RestApiResponse<Boolean> deleteBannerImg(@ApiParam(required = true, value = "banner图Id")@RequestParam int bannerImgId) {
        RestApiResponse<Boolean> result = new RestApiResponse<Boolean>();
        try {
            BannerImgEntity bannerImgEntity = bannerImgService.delete(bannerImgId);
            if (bannerImgEntity == null) {
                result.failedApiResponse(Const.FAILED, "删除失败");
                return result;
            }
            result.successResponse(Const.SUCCESS, true,"删除成功");
        } catch (Exception e) {
            logger.warn("删除banner图异常", e);
            result.failedApiResponse(Const.FAILED, "删除banner图异常");
        }
        return result;
    }

}
